package it.antonio.ilpizze.db.core;

import it.antonio.ilpizze.db.Maintenance;

public class MaintenanceManager implements Maintenance {
	
	private DbFileSystem dbfs;
	private RecordManager recordManager;
	private IndexManager indexManager;
	private SecurityDbManager securityManager;
	private StatusManager statusManager;
	private LockManager lockManager;
	
	public MaintenanceManager(DbFileSystem dbfs, RecordManager recordManager, IndexManager indexManager, SecurityDbManager securityManager, StatusManager statusManager, LockManager lockManager) {
		this.dbfs = dbfs;
		this.recordManager = recordManager;
		this.indexManager = indexManager;
		this.securityManager = securityManager;
		this.statusManager = statusManager;
		this.lockManager = lockManager;
	}




	@Override
	public void run() {
		statusManager.checkStatus(Status.OPEN);
		lockManager.checkMultiThreadingWrite();
		
		recordManager.runMaintenance(dbfs);
		indexManager.runMaintenance();
		securityManager.runMaintenance(dbfs);
		
		statusManager.status(Status.NEED_RESTART);
		dbfs.closeAllFiles();
		dbfs.unlockDb();
		
	}

}
